﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;

namespace NetworkChangeDetector
{
    public partial class ZuneDetectionPage : PhoneApplicationPage
    {
        ZuneDetector zd;

        public ObservableCollection<string> NetworkInterfaces { get; set; }

        public ObservableCollection<string> NetworkChanges { get; set; }

        public string ZuneStatusMsg
        {
            get
            {
                if (zd.IsZuneConnected)
                    return "Zune is connected";
                else
                    return "Zune not connected";
            }
        }

        public ZuneDetectionPage()
        {
            InitializeComponent();

            zd = new ZuneDetector();
            zd.OnZuneConnected += new EventHandler<NetworkEventArgs>(zd_OnZuneConnected);
            zd.OnZuneDisconnected += new EventHandler<NetworkEventArgs>(zd_OnZuneDisconnected);

            NetworkChanges = new ObservableCollection<string>();
            lbNetworkChanges.DataContext = NetworkChanges;

            NetworkInterfaces = new ObservableCollection<string>();
            lbNetworkInterfaces.DataContext = NetworkInterfaces;

            UpdateNetworkInterfaces();
            zd.CheckZuneConnection();

            btZuneConnetion.Content = ZuneStatusMsg;
        }

        private void zd_OnZuneDisconnected(object sender, NetworkEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                NetworkChanges.Add(e.Message);
                UpdateNetworkInterfaces();
                btZuneConnetion.Content = ZuneStatusMsg;
                btZuneConnetion.Background = new SolidColorBrush(Colors.Red);
            });
        }

        private void zd_OnZuneConnected(object sender, NetworkEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                NetworkChanges.Add(e.Message);
                UpdateNetworkInterfaces();
                btZuneConnetion.Content = ZuneStatusMsg;
                btZuneConnetion.Background = new SolidColorBrush(Colors.Green);
            });
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            UpdateNetworkInterfaces();
            zd.CheckZuneConnection();
            btZuneConnetion.Content = ZuneStatusMsg;
        }

        private void UpdateNetworkInterfaces()
        {
            NetworkInterfaces.Clear();
            NetworkInterfaceList networkInterfaceList = new NetworkInterfaceList();
            foreach (NetworkInterfaceInfo networkInterfaceInfo in networkInterfaceList)
            {
                string intf = String.Format(" {0}({1})",
                        networkInterfaceInfo.InterfaceName,
                        networkInterfaceInfo.InterfaceType.ToString());
                NetworkInterfaces.Add(intf);
            }
        }
    }
}